<template>
  <view class="bgColor" v-if="update" style="min-height: 100vh">
<!--    <view style="display: flex">-->
<!--      <view style="flex: 9">-->
<!--        <uni-card :title="order.userAddress.recipient" @click="addressFun" :extra="order.userAddress.phone">-->
<!--          <text class="uni-body">{{ order.userAddress.detailedAddress }}</text>-->
<!--        </uni-card>-->
<!--      </view>-->
<!--    </view>-->

    <view style="margin-top: 30rpx;width: 90%;display: flex;background-color: white;margin: auto;border-radius: 20rpx;padding-top: 30rpx;padding-bottom: 30rpx;padding-right: 20rpx;padding-left: 20rpx">
      <view style="flex: 1">
        <u-image radius="10" :src="imageUrl" width="170" height="170"></u-image>
      </view>
      <view style="flex: 3;padding-left: 10rpx">
        <view style="width: 100%;height: 80%;padding: 10rpx">
          <text style="font-weight: 600;font-size: 28rpx" class="textTile">{{order.commodityName}}</text>
        </view>

        <view style="width: 100%;position: relative;height: 20%;padding-left: 5rpx">
          <text style="font-size: 26rpx;color: rgb(154,152,152);margin-right: 20rpx;position: absolute;bottom: 20rpx;background-color: #D7D7D7FF;padding:6rpx 10rpx">{{order.specification}}</text>
          <uni-number-box :disabled="true"  :min="1" :max="9" style="position: absolute;bottom: 10rpx;right: 20rpx" @change="changeCount" :value="order.count" />
        </view>

      </view>
    </view>

    <view style="width: 90%;margin-left: auto;margin-right:auto;margin-top: 40rpx;background-color: white;border-radius: 20rpx;padding: 30rpx">



      <view style="width: 100%;margin-top: 20rpx;display: flex" v-for="(item,index) in info" :key="index" v-if="item.value">
        <view style="flex: 1">
          <text style="">{{item.title}}</text>
        </view>
        <view style="flex: 2">
          <view style="float: right">
            <text style="font-size: 20rpx;text-align: right" v-if="item.paramName.indexOf('price')!=-1">￥</text>
            <text style="text-align: right;position: center;color: #b9b9b9;font-size: 25rpx;">{{item.value}}</text>
          </view>

        </view>
      </view>
    </view>

<!--    <mi-loading-->
<!--        ref="Loading"-->
<!--        title="加载中"-->
<!--        hasMask="true"-->
<!--    ></mi-loading>-->
  </view>
</template>

<script>
import {mainService} from "../../../../api/buyer/main";
// import {BASE_IMG_URL} from "../../../api/service";
import {addressService} from "../../../../api/buyer/public/address";
export default {
  name: "OrderFrom",
  data() {
    return {
      user:{},
      addresses:[],
      order:{},
      imageUrl:"",
      update:true,
      info:[
        {
          title:"商品总价",
          value:"",
          paramName:"price"
        },
        {
          title:"收获信息",
          value:"",
          paramName:"deliveryInfo"
        },
        {
          title:"订单编号",
          value:"",
          paramName:"code"
        },
        {
          title:"创建时间",
          value:"",
          paramName:"createDatetime"
        },
        {
          title:"付款时间",
          value:"",
          paramName:"payTime"
        },
        {
          title:"发货时间",
          value:"",
          paramName:"deliveryTime"
        },
        {
          title:"成交时间",
          value:"",
          paramName:"signTime"
        },
        {
          title:"完成时间",
          value:"",
          paramName:"completeTime"
        },
        // {
        //   title:"订单编号",
        //   value:"",
        //   paramName:"code"
        // }
      ],
    }
  },
  created(){
    this.user = uni.getStorageSync('userInfo')
    if (!this.user){
      //未登录
      // uni.showToast({
      //   title: '未登录，请去登录',
      //   icon: 'none'
      // });
      //成功后延迟1秒跳转
      setTimeout(() => {
        uni.navigateTo({
          url:"/pages/public/login/login",
          success:res =>{},
          fail:() =>{},
          complete:() => {}
        });
      }, 1000);
    }else{
      this.order.userId=this.user.eid
    }
  },
  onLoad(option) {
    const s = decodeURIComponent(option.info);
    let info = JSON.parse(s)
    this.init(info.orderId);
    this.imageUrl=info.commodityUrl;
  },
  onShow() {
    let that = this
    uni.$on('updateSelectAddress',function(selectAddress){
      that.order.userAddress=selectAddress;
      that.order.userAddressId=selectAddress.eid;
      that.reload();
    })
  },
  methods:{
    reload(){
      this.update=false
      this.update=true
    },
    init(orderId){

      mainService.findOrderForView(orderId).then(res=>{
        this.order=res.data;
        // console.log(res.data)
        // addressService.findShippingAddressForView(this.order.userAddressId).then(res=>{
        //   this.order.userAddress=res.data;
        //   this.reload();
        // })
        this.info.forEach(item=>{
          if(item.paramName=='deliveryInfo'){
            item.value=this.order.recipient+","+this.order.phone+","+this.order.detailedAddress
            return;
          }
          item.value=this.order[item.paramName];
        })
        this.reload();
      })
    },
    changeCount(count){
      this.order.count=count;
      this.order.price=this.order.good.price*count;
      console.log("changeCount",this.order)
      this.reload();
    },
    // putOrder(){
    //   this.$refs.Loading.show() // 打开
    //   mainService.putOrder(this.order).then((res)=>{
    //     this.$refs.Loading.hide() // 关闭
    //     console.log(res.data)
    //     if(res.data.code==0){
    //       this.order.eid=res.data.orderId;
    //       this.goPay(this.order)
    //     }else {
    //       uni.showToast({
    //         title: '提交订单异常，请稍后再试',
    //         icon: 'none'
    //       });
    //     }
    //   }).catch((err)=>{
    //     this.$refs.Loading.hide() // 关闭
    //     uni.showToast({
    //       title: '提交订单异常，请稍后再试',
    //       icon: 'none'
    //     });
    //   })
    // },
    goPay(order){
      let orderInfo={
        price: order.count*order.good.price,
        orderId:order.eid
      }
      uni.navigateTo({
        url: '/pages/buyer/public/PayPage?orderInfo='+encodeURIComponent(JSON.stringify(orderInfo)),
        // url: '/pages/staff/application/production/taskList/task-detail'
      })
    },
    addressFun(){
      uni.navigateTo({
        url: '/pages/buyer/public/Address',
        // url: '/pages/staff/application/production/taskList/task-detail'
      })
    }
  }
}
</script>

<style scoped>
.button-bottom {
  position: fixed;
  bottom: calc(var(--window-bottom));
  z-index: 1030;
}
.textTile{
  overflow: hidden;
  -webkit-line-clamp: 2;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
}
</style>